package com.thh.zookeeperlock.zk;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

public class ZkWatchDemo {

    public static void main(String[] args){
        // 创建一个zk的客户端
        ZkClient client = new ZkClient("localhost:2181");

        client.setZkSerializer(new MyZkSerializer());//非常简单的一个序列化实现

        // 监听节点的变化和通知
        client.subscribeDataChanges("/zklock",new IZkDataListener(){
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("====================收到节点数据变化事件：" + data+"=========");
            }

            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println("====================收到节点被删除的事件===================");
            }
        });

        client.subscribeChildChanges("/zklock", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println("====================收到子节点数据变化事件：" + currentChilds+"=========");
            }
        });

        try{
            Thread.currentThread().join();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

}
